/*
 * @Date         : 2024-05-13 17:53:28 星期1
 * @Author       : xut
 * @Description  : 以下消息输出顺序
                    server_d_post start
                    server_c_post start
                    server_c_pre onReceiveMetadata
                    server_d_pre onReceiveMetadata
                    server_c_pre onReceiveMessage
                    server_d_pre onReceiveMessage
                    server_c_pre onReceiveHalfClose
                    server_d_pre onReceiveHalfClose
                    Timestamp from metadata:
                    0. 2024-05-13T11:11:15.976Z
                    server_d_post sendMetadata
                    server_c_post sendMetadata
                    server_d_post sendMessage
                    server_c_post sendMessage
                    server_d_post sendStatus
                    server_c_post sendStatus
                    server_c_pre onReceiveCancel
                    server_d_pre onReceiveCancel
                    server received cancelled event
 */
import grpc from "@grpc/grpc-js"

export const interceptor_c = (methodDescriptor, call) => {
  const server_c_pre = new grpc.ServerListenerBuilder()
    // 接收请求元数据时触发
    .withOnReceiveMetadata((metadata, next) => {
      console.log("server_c_pre onReceiveMetadata")
      next(metadata)
    })
    // 接收到请求消息流之前触发
    .withOnReceiveMessage((message, next) => {
      console.log("server_c_pre onReceiveMessage")
      next(message)
    })
    // 接收完请求消息流之后触发
    .withOnReceiveHalfClose((next) => {
      console.log("server_c_pre onReceiveHalfClose")
      next()
    })
    // 客户端请求取消时触发
    .withOnCancel(() => {
      console.log("server_c_pre onReceiveCancel")
    })
    .build()

  const server_c_post = new grpc.ResponderBuilder()
    // 服务端服务初始化时
    .withStart((next) => {
      console.log("server_c_post start")
      next(server_c_pre)
    })
    // 响应头元数据时
    .withSendMetadata((metadata, next) => {
      console.log("server_c_post sendMetadata")
      next(metadata)
    })
    // 响应消息发送时触发
    .withSendMessage((message, next) => {
      console.log("server_c_post sendMessage")
      next(message)
    })
    // 响应状态变更时触发
    .withSendStatus((status, next) => {
      console.log("server_c_post sendStatus")
      next(status)
    })
    .build()

  return new grpc.ServerInterceptingCall(call, server_c_post)
}

export const interceptor_d = (methodDescriptor, call) => {
  const server_d_pre = new grpc.ServerListenerBuilder()
    .withOnReceiveMetadata((metadata, next) => {
      console.log("server_d_pre onReceiveMetadata")
      next(metadata)
    })
    .withOnReceiveMessage((message, next) => {
      console.log("server_d_pre onReceiveMessage")
      next(message)
    })
    .withOnReceiveHalfClose((next) => {
      console.log("server_d_pre onReceiveHalfClose")
      next()
    })
    .withOnCancel(() => {
      console.log("server_d_pre onReceiveCancel")
    })
    .build()

  const server_d_post = new grpc.ResponderBuilder()
    .withStart((next) => {
      console.log("server_d_post start")
      next(server_d_pre)
    })
    .withSendMetadata((metadata, next) => {
      console.log("server_d_post sendMetadata")
      next(metadata)
    })
    .withSendMessage((message, next) => {
      console.log("server_d_post sendMessage")
      next(message)
    })
    .withSendStatus((status, next) => {
      console.log("server_d_post sendStatus")
      next(status)
    })
    .build()

  return new grpc.ServerInterceptingCall(call, server_d_post)
}
